﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class AddPrefects : System.Web.UI.Page
    {
        StudentAL studentAL = new StudentAL();


        public Guid SelectedPrefectBatch
        {
            get
            {
                if (Session["PrefectBatchId"] == null)
                {
                    return studentAL.GetAllPrefectBatches()[0].PrefectBatchId;
                }
                else
                {
                    return new Guid(Session["PrefectBatchId"].ToString());
                }
            }
            set
            {
                Session["PrefectBatchId"] = value;
            }
        }

        public Guid SelectedStudent
        {
            get
            {
                if (Session["StudentId"] == null)
                {
                    return studentAL.GetStudents()[0].StudentId;
                }
                else
                {
                    return new Guid(Session["StudentId"].ToString());
                }
            }
            set
            {
                Session["StudentId"] = value;
            }
        }


        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);

            List<PrefectBatchDTO> prefectBatchDTO = studentAL.GetAllPrefectBatches();
            ddlPrefctPrfctBtch.DataSource = prefectBatchDTO;
            ddlPrefctPrfctBtch.DataTextField = "PrefectBatchName";
            ddlPrefctPrfctBtch.DataValueField = "PrefectBatchId";
            ddlPrefctPrfctBtch.SelectedValue = SelectedPrefectBatch.ToString();
            ddlPrefctPrfctBtch.DataBind();

            List<StudentDTO> studentDTO = studentAL.GetStudents();
            ddlPrefectStudentName.DataSource = studentDTO;
            ddlPrefectStudentName.DataTextField = "FullName";
            ddlPrefectStudentName.DataValueField = "StudentId";
            ddlPrefectStudentName.SelectedValue = SelectedStudent.ToString();
            ddlPrefectStudentName.DataBind();

        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnPrefectSubmit_Click(object sender, EventArgs e)
        {
            StudentAL studentAL = new StudentAL();

            DateTime prefectFrm = DateTime.MinValue;
            DateTime prefectTo = DateTime.MinValue;

            DateTime prefectFrmDte = DateTime.MinValue;
            DateTime prefectToDte = DateTime.MinValue;

            int prefectFrmYr = 0;
            int prefectToYr = 0;

            bool convertableToDatePrfctFrm = DateTime.TryParse(txtPrefctFrmDate.Text, out prefectFrm);
            bool convertableToDatePrfcthTo = DateTime.TryParse(txtPrefctToDate.Text, out prefectTo);

            if (convertableToDatePrfctFrm)
            {
                prefectFrmDte = prefectFrm;
            }
            else
            {
                bool convertableToInt = Int32.TryParse(txtPrefctFrmDate.Text, out prefectFrmYr);
                //Year should be yyyy format
                if (convertableToInt && txtPrefctFrmDate.Text.Length == 4)
                {
                    prefectFrmDte = Convert.ToDateTime(txtPrefctFrmDate.Text + "-07-01");
                }
                else
                {
                   // Response.Redirect("AddPrefects.aspx?Entered Prefect's  From Year is not in valid format");
                    lblAddPrefects.Text = "Entered Prefect's  From Year is not in valid format";
                }
            }

            if (convertableToDatePrfcthTo)
            {
                prefectToDte = prefectTo;
            }
            else
            {
                bool convertableToInt = Int32.TryParse(txtPrefctToDate.Text, out prefectToYr);
                //Year should be yyyy format
                if (convertableToInt && txtPrefctToDate.Text.Length == 4)
                {
                    prefectToDte = Convert.ToDateTime(txtPrefctToDate.Text + "-06-30");
                }
                else
                {
                   // Response.Redirect("AddPrefects.aspx?Entered Prefect's  To Year is not in valid format");
                    lblAddPrefects.Text = "Entered Prefect's  To Year is not in valid format";
                }


                DateTime prefctBtchStart = Convert.ToDateTime( studentAL.GetPrefectBatchById(new Guid(ddlPrefctPrfctBtch.SelectedValue)).SingleOrDefault().PrefectFrom);
                DateTime prefctBtchEnd = Convert.ToDateTime( studentAL.GetPrefectBatchById(new Guid(ddlPrefctPrfctBtch.SelectedValue)).SingleOrDefault().PrefectTo);

                try
                {
                    if (txtPrefctFrmDate.Text == "" || txtPrefctToDate.Text == "")
                    {
                        // prefectFrmDte = commStart;
                        // prefectToDte = commEnd;
                        prefectFrmDte = prefctBtchStart;
                        prefectToDte = prefctBtchEnd;
                    }
                    else
                    {
                        //if you dont know prefct batch perios thn it's batch period
                       // prefectFrmDte = prefctBtchStart;
                       // prefectToDte = prefctBtchEnd;
                    }



                    PrefectDTO prefectDTO = new PrefectDTO
                    {
                        PrefecBatchtId = new Guid(ddlPrefctPrfctBtch.SelectedValue),
                        StudentId = new Guid(ddlPrefectStudentName.SelectedValue),
                        Role = ddlPrefectRole.SelectedValue,
                        PrefectFrom = prefectFrmDte,
                        PrefectTo = prefectToDte,
                        Description = txtPrefectDesc.Text,
                        //ModifiedBy="Tharsan",
                        ModifiedTime = System.DateTime.Now
                    };

                    string result = studentAL.SavePrefects(prefectDTO);
                    if (result == "Saved")
                    {
                        lblAddPrefects.Text = "The prefect Data Details added Successfully";
                        txtPrefectDesc.Text = "";
                        txtPrefctToDate.Text = "";
                        txtPrefctFrmDate.Text = "";




                    }
                    else if (result == "Updated")
                    {
                        lblAddPrefects.Text = "The prefect Data Details Updated Successfully";
                        txtPrefectDesc.Text = "";
                        txtPrefctToDate.Text = "";
                        txtPrefctFrmDate.Text = "";
                    }
                    else if (result == "InvalidPrefectPeriod")
                    {
                        lblAddPrefects.Text = "The Prefect's period didn't fit into the prefect's batch's period";
                        txtPrefectDesc.Text = "";
                        txtPrefctToDate.Text = "";
                        txtPrefctFrmDate.Text = "";
                    }
                    else if (result == "NoOfStudentsForThisRoleExceeded")
                    {
                        lblAddPrefects.Text = "The No Of Students for this role have been exceeded the limit";
                        txtPrefectDesc.Text = "";
                        txtPrefctToDate.Text = "";
                        txtPrefctFrmDate.Text = "";
                    }
                    else
                    {
                        lblAddPrefects.Text = "Your data cannot be added";
                        txtPrefectDesc.Text = "";
                        txtPrefctToDate.Text = "";
                        txtPrefctFrmDate.Text = "";
                    }
                }
                catch (Exception ee)
                {
                    lblAddPrefects.Text = "There is an exception occured while inserting the prefect Data details " + ee.ToString();
                    txtPrefectDesc.Text = "";
                    txtPrefctToDate.Text = "";
                    txtPrefctFrmDate.Text = "";

                }

            }

        }
        protected void btnPrefectCancel_Click(object sender, EventArgs e)
        {

        }

        protected void ddlPrefctPrfctBtch_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedPrefectBatch = new Guid(ddlPrefctPrfctBtch.SelectedValue);
        }

        protected void ddlPrefectStudentName_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedStudent = new Guid(ddlPrefectStudentName.SelectedValue);
        }

        protected void ddlPrefectRole_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
